package com.goldplusgold.tradedelay.app.market.channel;

import android.util.Log;

import com.goldplusgold.network.lib.http.netty.ChannelBootstrapManager;
import com.goldplusgold.network.lib.http.netty.ChannelManager;
import com.goldplusgold.network.lib.http.protobuf.HeartbeatInfo;
import com.goldplusgold.network.lib.http.protobuf.Market;
import com.goldplusgold.support.lib.utils.Logger;
import com.goldplusgold.tradedelay.app.market.MarketManager;
import com.goldplusgold.tradedelay.app.market.eventbus.ConnectionFailEvent;
import com.goldplusgold.tradedelay.app.market.eventbus.ConnectionSuccessEvent;
import com.goldplusgold.tradedelay.app.market.eventbus.DynaMessageEvent;
import com.goldplusgold.tradedelay.app.market.eventbus.FiveRangeMessageEvent;
import com.goldplusgold.tradedelay.app.market.eventbus.InstMessageEvent;
import com.goldplusgold.tradedelay.app.market.eventbus.SltpNotificationMessageEvent;

import org.greenrobot.eventbus.EventBus;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;


/**
 * 客户端行情处理器
 */
public class MarketPushInboundHandler extends ChannelInboundHandlerAdapter {

    public MarketPushInboundHandler() {
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        Market.Response data = (Market.Response) msg;
        switch (data.getIdentification().getCategory()) {
            case QUOTEFIVERANGE:
                EventBus.getDefault().post(new FiveRangeMessageEvent(Market.Category.QUOTEFIVERANGE,data.getIdentification(), data.getQuoteFiveRange()));
                break;
            case QUOTEMIN:
//                EventBus.getDefault().post(new MinuteMessageEvent(Market.Category.QUOTEMIN, data.getIdentification(), data.getQuoteMin(0)));
                MarketManager.getInstance().parseMinuteData(data.getQuoteMin(0));
                break;
            case HEARTBEAT:
                break;
            case QUOTEINST:
                EventBus.getDefault().post(new InstMessageEvent(Market.Category.QUOTEINST, data.getQuoteInst()));
                break;
            case QUOTEDYNA:
                EventBus.getDefault().post(new DynaMessageEvent(Market.Category.QUOTEDYNA, data.getIdentification(), data.getQuoteDyna()));
                break;
            case KLINEDAY:
                try {
                    MarketManager.getInstance().parseStickData(data.getKlineDay(0));
                }catch (Exception e){
                    Logger.e("parseStickData",e.toString());
                }
//                EventBus.getDefault().post(new MessageEvent(Market.Category.KLINEDAYPAGE,data.getIdentification(), data.getKlineDay(0)));
                break;
            case KLINEWEEK:
                try {
                    MarketManager.getInstance().parseStickData(data.getKlineWeek(0));
                }catch (Exception e){
                    Logger.e("parseStickData",e.toString());
                }
//                EventBus.getDefault().post(new MessageEvent(Market.Category.KLINEWEEKPAGE,data.getIdentification(), data.getKlineWeek(0)));
                break;
            case KLINEMONTH:
                try {
                    MarketManager.getInstance().parseStickData(data.getKlineMonth(0));
                }catch (Exception e){
                    Logger.e("parseStickData",e.toString());
                }
//                EventBus.getDefault().post(new MessageEvent(Market.Category.KLINEMONTHPAGE,data.getIdentification(), data.getKlineMonth(0)));
                break;
            case KLINEMIN1:
                try {
                    MarketManager.getInstance().parseStickData(data.getKlineMin1(0));
                }catch (Exception e){
                    Logger.e("parseStickData",e.toString());
                }
//                EventBus.getDefault().post(new MessageEvent(Market.Category.KLINEMIN1PAGE,data.getIdentification(), data.getKlineMin1(0)));
                break;
            case KLINEMIN5:
                try {
                    MarketManager.getInstance().parseStickData(data.getKlineMin5(0));
                }catch (Exception e){
                    Logger.e("parseStickData",e.toString());
                }
//                EventBus.getDefault().post(new MessageEvent(Market.Category.KLINEMIN5PAGE,data.getIdentification(), data.getKlineMin5(0)));
                break;
            case KLINEMIN15:
                try {
                    MarketManager.getInstance().parseStickData(data.getKlineMin15(0));
                }catch (Exception e){
                    Logger.e("parseStickData",e.toString());
                }
//                EventBus.getDefault().post(new MessageEvent(Market.Category.KLINEMIN15PAGE,data.getIdentification(), data.getKlineMin15(0)));
                break;
            case KLINEMIN30:
                try {
                    MarketManager.getInstance().parseStickData(data.getKlineMin30(0));
                }catch (Exception e){
                    Logger.e("parseStickData",e.toString());
                }
//                EventBus.getDefault().post(new MessageEvent(Market.Category.KLINEMIN30PAGE,data.getIdentification(), data.getKlineMin30(0)));
                break;
            case KLINEMIN60:
                try {
                    MarketManager.getInstance().parseStickData(data.getKlineMin60(0));
                }catch (Exception e){
                    Logger.e("parseStickData",e.toString());
                }
//                EventBus.getDefault().post(new MessageEvent(Market.Category.KLINEMIN60PAGE,data.getIdentification(), data.getKlineMin60(0)));
                break;
            case KLINEMIN240:
                try {
                    MarketManager.getInstance().parseStickData(data.getKlineMin240(0));
                }catch (Exception e){
                    Logger.e("parseStickData",e.toString());
                }
//                EventBus.getDefault().post(new MessageEvent(Market.Category.KLINEMIN240PAGE,data.getIdentification(), data.getKlineMin240(0)));
                break;
            case SLTPNOTIFICATION:
                EventBus.getDefault().post(new SltpNotificationMessageEvent(data.getIdentification(), data.getSltpNotification()));
                break;
        }
    }


    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        EventBus.getDefault().post(new ConnectionSuccessEvent());
        Market.Heartbeat heartbeat = Market.Heartbeat.newBuilder().
                setInfo(HeartbeatInfo.REQUEST.toInfo()).build();
        Market.Identification identification = Market.Identification.newBuilder().
                setCategory(Market.Category.HEARTBEAT).build();
        Market.Response data = Market.Response.newBuilder().setHeartbeat(heartbeat).
                setIdentification(identification).build();
        ctx.channel().writeAndFlush(data);
        ChannelManager.addSession(100175, ctx.channel());
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        Log.e("exceptionCaught",cause.toString());
        ctx.channel().close();
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        super.channelInactive(ctx);
        Log.e("lf", "close");
        EventBus.getDefault().post(new ConnectionFailEvent());
        ChannelBootstrapManager.getInstance().unregister(MarketClientBootstrap.class.getName());
        ChannelBootstrapManager.getInstance().register(MarketClientBootstrap.class.getName(),new MarketClientBootstrap());
    }
}
